package com.example.patient.controller;

import com.example.dto.patient.PatientAlterDTO;
import com.example.dto.patient.PatientDeleteDTO;
import com.example.dto.patient.PatientEditDTO;
import com.example.dto.patient.PatientQueryDTO;
import com.example.patient.entity.Patient;
import com.example.patient.service.ICampusService;
import com.example.patient.service.IPatientInformationService;
import com.example.vo.patient.CampusVO;
import com.example.vo.patient.PatientVo;
import com.example.vo.patient.Result;
import com.example.vo.JsonVO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xw
 * @since 2024-05-09
 */
@RestController
@RequestMapping("/patient")
public class PatientInformationController {

    private final static Logger log = LoggerFactory.getLogger(PatientInformationController.class);

    @Autowired
    private IPatientInformationService patientInformationService;

    @Autowired
    ICampusService campusService;

    @GetMapping("/all")
    public JsonVO<List<CampusVO>> all(){
        return JsonVO.success(campusService.all_dep_search());
    }

    @PostMapping("/query")
    public JsonVO<List<Patient>> query(
            @RequestBody PatientQueryDTO patientQueryDTO) {
        List<Patient> result=patientInformationService.patientList(patientQueryDTO);
        if(result.isEmpty()){
            return JsonVO.fail("没有查询到数据");
        }else{
            return JsonVO.success(result);
        }
    }



//    @PostMapping("/add")    //新增患者
//    public JsonVO<String> add(@RequestHeader("userId") String userId, @RequestBody PatientAlterDTO patientAlterDTO){
//        try {
//        Result<String> result = patientInformationService.patientAdd(userId, patientAlterDTO);
//        if(result.isStatus()) return JsonVO.success(result.getMessage());
//        return JsonVO.fail(result.getMessage());
//        } catch (Exception e) {
//            return JsonVO.fail(e.getMessage());
//        }
//    }

    @PostMapping("/adds")
    public JsonVO<String> adds(@RequestHeader("userId") String userId,@RequestBody List<PatientAlterDTO> patientList) {
        try {
            // 批量导入患者信息
            Result<String> result = patientInformationService.patientsAdd(userId,patientList);
            if (result.isStatus()) return JsonVO.success(result.getMessage());
            return JsonVO.fail(result.getMessage());
        } catch (Exception e) {
            return JsonVO.fail(e.getMessage());
        }
    }



    @PostMapping ("/edit")    //编辑患者
    public JsonVO<String> edit(@RequestHeader("userId") String userId,@RequestBody PatientEditDTO patientEditDTO){
        try {
        Result<String> result = patientInformationService.patientEdit(userId,patientEditDTO);
        if(result.isStatus())return JsonVO.success(result.getMessage());
        return JsonVO.fail(result.getMessage());
        } catch (Exception e) {
            return JsonVO.fail( e.getMessage());
        }
    }

    @PostMapping("/delete")  //删除患者（物理删除）
    public JsonVO<String> delete(@RequestBody PatientDeleteDTO patientDeleteDTO){
        try {
        Result<String> result = patientInformationService.patientDelete(patientDeleteDTO);
        if(result.isStatus()) return JsonVO.success(result.getMessage());
        return JsonVO.fail(result.getMessage());
        } catch (Exception e) {
            return JsonVO.fail( e.getMessage());
        }
    }
}
